package com.sancai.fruit.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.github.nukc.stateview.StateView;
import com.sancai.fruit.R;
import com.sancai.fruit.utils.IToast;
import com.zhy.autolayout.AutoLayoutActivity;

import butterknife.ButterKnife;

public abstract class BaseActivity<T extends BasePresenterIm, V> extends AutoLayoutActivity implements BaseView<V> {
    public String TAG = "xy_activity";
    public T mPresenter;
    private StateView mStateView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContenView());
        initStateLayout();
        ButterKnife.bind(this);
        if (null != getPresenter()) {
            mPresenter = getPresenter();
        }
        initView();
        if (null != getPresenter()) {
            mPresenter = getPresenter();
            mPresenter.attachView(this, this);
        }
        initData();


    }


    /**
     * 注入statelayout
     */
    private void initStateLayout() {
        View view = injectTarget();
        if (view == null) {
            return;
        }
        mStateView = StateView.inject(injectTarget());
        mStateView.setOnRetryClickListener(() -> {
            //点击重试响应事件
            reloadActivity();
        });
    }

    /**
     * 点击重试要做的操作
     */
    protected abstract void reloadActivity();

    /**
     * 注入要替换的View
     */
    protected abstract View injectTarget();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化View
     */
    protected abstract void initView();

    /**
     * 页面布局
     */
    public abstract int getContenView();

    @Override
    public void showDataError(String errorMessage) {
        mStateView.showRetry();

    }

    @Override
    public abstract void showDataSuccess(V datas);


    @Override
    public void showLoadingView() {
        mStateView.showLoading();

    }

    @Override
    public void showNetErrorView() {
        mStateView.showNetError();
    }

    @Override
    public void showEmptyView(String msg) {
        mStateView.showEmpty(msg);

    }

    @Override
    public void showContentView() {
        mStateView.showContent();
    }

    @Override
    public void showRetryView() {
        mStateView.showRetry();
    }

    public T getPresenter() {
        return mPresenter;
    }

    ;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unsubcrible();
        }
        unRegisterSomething();
    }

    /**
     * 解除注册一些东西
     */
    public void unRegisterSomething() {

    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
        //定义转场动画
        overridePendingTransition(R.anim.enter, R.anim.exit);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        //定义转场动画
        overridePendingTransition(R.anim.enter, R.anim.exit);
    }

    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        IToast.show(this, text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        IToast.show(this, getString(resId));
    }

    @Override
    public void showCommentEmptyView() {

    }

    @Override
    public void showDiyView(int imgRes, String msg) {

    }
}
